#include "SystemSettingUi.h"
#include "ui_SystemSettingUi.h"
#include "WidgetHandler.h"
#include "MiscData.h"
#include "ResetFactoryDialog.h"
#include "LogoUtility.h"
#include "UpdateDialog.h"
#include "FileUtility.h"
#include "HintDialog.h"
#include "AuthorityDialog.h"
#include "Mcu.h"
#include "App.h"
#include <QProcess>
#include <QTranslator>


SystemSettingUi::SystemSettingUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::SystemSettingUi)
{
    ui->setupUi(this);
    ui->userId->installEventFilter(this);
    LogoUtility::getInstance()->addLogoLabel(ui->logo);
    g_countTimer = new QTimer();
    QObject::connect(g_countTimer, &QTimer::timeout, this, &SystemSettingUi::countTimerSlot);
    g_countTimer->setSingleShot(true);
    //ui->label_9->hide();
    //ui->touchSound->hide();
    QObject::connect(this, &SystemSettingUi::clickedUserIdSignal, this, &SystemSettingUi::clickedUserIdSlot);

    QObject::connect(AuthorityDialog::getInstance(), &AuthorityDialog::SystemSettingUi_ResetFactory_Signal,
                     this, &SystemSettingUi::SystemSettingUi_ResetFactory_Slot);

    QObject::connect(AuthorityDialog::getInstance(), &AuthorityDialog::SystemSettingUi_UpdateSoftware_Signal,
                     this, &SystemSettingUi::SystemSettingUi_UpdateSoftware_Slot);

    QObject::connect(AuthorityDialog::getInstance(), &AuthorityDialog::SystemSettingUi_Logoin_Signal,
                     this, &SystemSettingUi::SystemSettingUi_Logoin_Slot);

    QObject::connect(HintDialog::getInstance(), &HintDialog::SystemSettingUi_QuitLogoin_Signal,
                     this, &SystemSettingUi::SystemSettingUi_QuitLogoin_Slot);
    QObject::connect(ui->selectLanguage,SIGNAL(currentIndexChanged(int )),
                     this,SLOT(langChanged(int )));
    ui->dateTimeWidget->hide();

    m_lanindex = 0;
}


void SystemSettingUi::changeEvent(QEvent *e) {

    int userId = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId;
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        if (userId < 0) {
            ui->userId->setText(tr("未登录(点击登录)"));
        } else {
            ui->userId->setText("admin"+tr("(点击退出)"));
        }
        return;
    default:
        break;
    }
    QFrame::changeEvent(e);

}

SystemSettingUi::~SystemSettingUi()
{
    delete ui;
}

void SystemSettingUi::activate()
{
    int userId = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId;

    if (userId < 0) {
        ui->userId->setText(tr("未登录(点击登录)"));
    } else {
        ui->userId->setText("admin"+tr("(点击退出)"));
    }

    ui->version->setText(SYSTEM_VERSION);


}

void SystemSettingUi::modifyLanByfile() {
    QFile file("/home/root/lan.data");
    bool ret = file.open(QIODevice::ReadOnly);
    qDebug()<<ret;
    int index;
    if(ret) {
        QDataStream out(&file);
        out>>index;
        qDebug()<<"curlan"<<index;
        file.close();
        ui->selectLanguage->setCurrentIndex(index-1);
    }

}

void SystemSettingUi::modifyLanByindex(int index) {
    ui->selectLanguage->setCurrentIndex(index);
}

bool SystemSettingUi::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->userId && event->type() == QEvent::MouseButtonPress) {
        Mcu::getInstance()->touchSoundBeepMessage();
        emit clickedUserIdSignal();
    }


    return QFrame::eventFilter(watched, event);
}

void SystemSettingUi::on_gohome_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void SystemSettingUi::on_goback_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void SystemSettingUi::on_version_clicked()
{
    if (g_versionClickedCount == 0)
        Mcu::getInstance()->touchSoundBeepMessage();
    g_versionClickedCount++;
    g_countTimer->start(400);
}

void SystemSettingUi::countTimerSlot()
{
    if (g_versionClickedCount >= 4) {
        WidgetHandler::getInstance()->displayWidget(EngineerUi_Id);
    }
    g_versionClickedCount = 0;
}

void SystemSettingUi::on_resetFactory_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();

    int userId = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId;

    if (userId < 0) {
        AuthorityDialog::getInstance()->setAuthorityType(AuthorityType_SystemSettingUi_ResetFactory);
        AuthorityDialog::getInstance()->setParent(this);
        AuthorityDialog::getInstance()->exec();
    } else {
        SystemSettingUi_ResetFactory_Slot();
    }
}

void SystemSettingUi::on_updateSoftware_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();

    int userId = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId;

    if (userId < 0) {
        AuthorityDialog::getInstance()->setParent(this);
        AuthorityDialog::getInstance()->setAuthorityType(AuthorityType_SystemSettingUi_UpdateSoftware);
        AuthorityDialog::getInstance()->exec();
    } else {
        SystemSettingUi_UpdateSoftware_Slot();
    }
}

void SystemSettingUi::SystemSettingUi_Logoin_Slot()
{
    ui->userId->setText(tr("admin")+tr("(点击退出)"));
}

void SystemSettingUi::SystemSettingUi_ResetFactory_Slot()
{
    ResetFactoryDialog::getInstance()->exec();
}

void SystemSettingUi::SystemSettingUi_UpdateSoftware_Slot()
{
    if (FileUtility::getInstance()->checkUsbUpdateDirExist("/run/media") < 0) {
        HintDialog::getInstance()->setInfo(tr("未找到外部存储设备!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }
    UpdateDialog::getInstance()->exec();
}

void SystemSettingUi::SystemSettingUi_QuitLogoin_Slot() {
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId = -1;
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.loginMode = 0;
    RuntimeData::BootSetting::getInstance()->saveBootSettingInfoToRom();
    ui->userId->setText(tr("未登录(点击登录)"));
}

void SystemSettingUi::clickedUserIdSlot() {
    int userId = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId;

    if (userId < 0) {
        AuthorityDialog::getInstance()->setParent(this);
        AuthorityDialog::getInstance()->setAuthorityType(AuthorityType_SystemSettingUi_Logoin);
        AuthorityDialog::getInstance()->exec();
    } else {
        HintDialog::getInstance()->setInfo(tr("是否退出登录,同时会解除自动登录?"), SystemSettingUi_QuitLogoin);
        HintDialog::getInstance()->exec();
    }
}

void SystemSettingUi::on_settingData_clicked()
{

    ui->dateTimeWidget->show();
    ui->dateEdit->setText(QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd"));
    ui->timeEdit->setText(QDateTime::currentDateTimeUtc().toString("hh:mm:ss"));
    Mcu::getInstance()->touchSoundBeepMessage();
}

void SystemSettingUi::on_touchSound_clicked(bool /*checked*/) {

}

void SystemSettingUi::on_saveDateTimeButton_clicked(){
    QString date = ui->dateEdit->text();
    QString time = ui->timeEdit->text();
    QString m=QString::asprintf("date -s \"%s %s\"",
                                date.toStdString().data(),time.toStdString().data());

    QProcess::startDetached(m); // 0312 表示3月12日，1025表示当前时分
    QProcess::startDetached("hwclock -w"); // 同步系统时间
    QProcess::startDetached("sync"); // 保存配置
    ui->dateTimeWidget->hide();
   // OperateStatusLabel::getInstance()->displayOperateInfo(QString(tr("set time succeed")));
}

void SystemSettingUi::langChanged(int index) {
        changCurrentLanguage(index+1);
}


